package com.czy.owner.ui.workorder.viewholder;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.czy.owner.R;
import com.czy.owner.db.UserHelper;
import com.czy.owner.entity.MyOrderModel;
import com.czy.owner.global.Constants;
import com.czy.owner.net.utils.JsonUtil;
import com.czy.owner.ui.workorder.SubscribeOrdelActivity;
import com.czy.owner.utils.MyLog;
import com.czy.owner.utils.PhoneUtils;
import com.czy.owner.utils.SharedPreferencesUtils;
import com.czy.owner.utils.TimeUtils;
import com.czy.owner.widget.UniversalDialog;
import com.easyrecycleview.adapter.BaseViewHolder;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;


/**
 * Created wangyang on 2016/9/8.
 */
public class OrderCancelledViewHolder extends BaseViewHolder<MyOrderModel> {
    private TextView tv_order_number;
    private TextView tv_order_item;
    private TextView tv_order_service_time;
    private TextView tv_order_jishi;
    private TextView tv_order_had_money;
    private TextView tv_order_other;
    private Button btn_order_paid;
    private Context ctx;


    public OrderCancelledViewHolder(ViewGroup parent, Context ctx) {
        super(parent, R.layout.item_order_cancelled);
        tv_order_number = $(R.id.tv_order_number);
        tv_order_item = $(R.id.tv_order_item);
        tv_order_service_time = $(R.id.tv_order_service_time);
        tv_order_jishi = $(R.id.tv_order_jishi);
        tv_order_had_money = $(R.id.tv_order_had_money);
        tv_order_other = $(R.id.tv_order_other);
        btn_order_paid = $(R.id.btn_order_paid);
        this.ctx = ctx;
    }

    @Override
    public void setData(final MyOrderModel model) {
        tv_order_number.setText("订单号: " + model.getOrderNumber());
        tv_order_item.setText(model.getServiceList());
        String time = TimeUtils.formatTimeDifference2(model.getServiceTime());
        tv_order_service_time.setText(time);
        tv_order_jishi.setText(model.getOrderEmps());
        tv_order_other.setText(model.getNote());
        tv_order_had_money.setText("￥" + String.format("%.2f", model.getActualDepositAmount()));


        btn_order_paid.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DeleteOrderDialog(model, "删除订单", "确定", "取消");

            }
        });


    }

    private void DeleteOrderDialog(final MyOrderModel model, String title, String sure, String cancle) {
        new UniversalDialog(ctx).builder().setTitle(title)
                .setPositiveButton(sure, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        deleteOrder(model);
                    }
                }).setNegativeButton(cancle, new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        }).show();
    }

    private void deleteOrder(final MyOrderModel model) {
        String url = Constants.APP_API_BASE + Constants.OWN_ORDER_ORDERDELORDE;
        RequestParams params = new RequestParams(url);
        params.addBodyParameter("session", UserHelper.getInstance().getSessionInfoModel(ctx).getSession());
        params.addBodyParameter("storeId",model.getStoreId()+"");
        params.addBodyParameter("orderNumber", model.getOrderNumber());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                MyLog.e("yang", "onSuccess==" + result);
                String flag = JsonUtil.getJsonValuesString(result, "flag");
                String exp = JsonUtil.getJsonValuesString(result, "exp");
                if (flag.equals("true")) {
                    //EventBus.getDefault().post(new UpdateOrderMessageEvent("订单统计"));
                    Intent intent = new Intent();
                    intent.setAction(SubscribeOrdelActivity.orderAction);
                    if (SubscribeOrdelActivity.currentId() == 0) {
                        intent.putExtra("currentId", 0);
                    } else {
                        intent.putExtra("currentId", 4);
                    }
                    ctx.sendBroadcast(intent);
                } else {
                    PhoneUtils.ShowToastMessage(ctx, exp);
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                MyLog.e("yang", "onError==" + ex.toString());
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
